package org.primefaces.examples.view;

import com.himilco.ocana.workflow.core.bo.ServiceProviderBO;
import com.himilco.ocana.workflow.core.dao.entities.Ticket;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;

@ManagedBean(name = "tableBean2") 
@SessionScoped
public class TableBean implements Serializable {

    private boolean value2;
    private String[] colors;
    private String[] manufacturers;
    
    private List<Car> carsSmall;
    private List <Ticket> ticketsDispatched;
    
    private Car selectedCar;
    private Ticket selectedTicket;
    private Car[] selectedCars;
    private Ticket[] TicketsSelected;
    
    @EJB
    private ServiceProviderBO spBO;
    
    public TableBean() {
        colors = new String[10];
        colors[0] = "Black";
        colors[1] = "White";
        colors[2] = "Green";
        colors[3] = "Red";
        colors[4] = "Blue";
        colors[5] = "Orange";
        colors[6] = "Silver";
        colors[7] = "Yellow";
        colors[8] = "Brown";
        colors[9] = "Maroon";

        manufacturers = new String[10];
        manufacturers[0] = "Mercedes";
        manufacturers[1] = "BMW";
        manufacturers[2] = "Volvo";
        manufacturers[3] = "Audi";
        manufacturers[4] = "Renault";
        manufacturers[5] = "Opel";
        manufacturers[6] = "Volkswagen";
        manufacturers[7] = "Chrysler";
        manufacturers[8] = "Ferrari";
        manufacturers[9] = "Ford";
       
        carsSmall = new ArrayList<Car>();
        ticketsDispatched = new ArrayList<Ticket>();
        populateRandomCars(carsSmall, 9);
    }

    public Car getSelectedCar() {
        System.out.println("invodekd");
        return selectedCar;
    }

    public void setSelectedCar(Car selectedCar) {
        this.selectedCar = selectedCar;
    }

    public List<Car> getCarsSmall() {
        return carsSmall;
    }

    public void setCarsSmall(List<Car> carsSmall) {
        this.carsSmall = carsSmall;
    }

    public Car[] getSelectedCars() {
        return selectedCars;
    }

    public void setSelectedCars(Car[] selectedCars) {
        this.selectedCars = selectedCars;
    }

    private void populateRandomCars(List<Car> list, int size) {
        for (int i = 0; i < size; i++) {
            list.add(new Car(getRandomModel(), getRandomYear(), getRandomManufacturer(), getRandomColor()));
        }
    }

    private int getRandomYear() {
        return (int) (Math.random() * 50 + 1960);
    }

    private String getRandomColor() {
        return colors[(int) (Math.random() * 10)];
    }

    private String getRandomManufacturer() {
        return manufacturers[(int) (Math.random() * 10)];
    }

    private String getRandomModel() {
        return UUID.randomUUID().toString().substring(0, 8);
    }

    public boolean isValue2() {
        return value2;
    }

    public void setValue2(boolean value2) {
        this.value2 = value2;
    }

    public void addMessage() {
        String summary = value2 ? "Checked" : "Unchecked";

        FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(summary));
    }

    public void onRowSelect() {
    }

    public Ticket[] getTicketsSelected() {
        return TicketsSelected;
    }

    public void setTicketsSelected(Ticket[] TicketsSelected) {
        this.TicketsSelected = TicketsSelected;
    }

    public String[] getColors() {
        return colors;
    }

    public void setColors(String[] colors) {
        this.colors = colors;
    }

    public String[] getManufacturers() {
        return manufacturers;
    }

    public void setManufacturers(String[] manufacturers) {
        this.manufacturers = manufacturers;
    }

    public Ticket getSelectedTicket() {
        return selectedTicket;
    }

    public void setSelectedTicket(Ticket selectedTicket) {
        this.selectedTicket = selectedTicket;
    }

   

    public ServiceProviderBO getSpBO() {
        return spBO;
    }

    public void setSpBO(ServiceProviderBO spBO) {
        this.spBO = spBO;
    }

    public List<Ticket> getTicketsDispatched() {
        //return ticketsDispatched;
        ticketsDispatched.addAll(spBO.findAllDispatchedTickets()); 
        return ticketsDispatched;
    }

    public void setTicketsDispatched(List<Ticket> ticketsDispatched) {
        this.ticketsDispatched = ticketsDispatched;
    }
    
}
